package cn.hznc.controller.service.analysis;

import cn.hutool.core.util.ObjectUtil;
import cn.hznc.domain.request.analysis.ToolTypeAnalysisReq;
import cn.hznc.repository.EntryExitRecordRepository;
import cn.hznc.repository.ToolForkInfoRespository;
import cn.hznc.repository.ToolPartRespository;
import cn.hznc.utils.CommonUtil;
import cn.hznc.vo.ToolTypeAnalysisSimpleVo;
import cn.hznc.vo.ToolTypeAnalysisVo;
import cn.hznc.vo.ToolTypeStockVo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author：Administrator
 * @Date：2023/6/28 15:01
 */
@Service
public class ToolAnalysisService{


    @Resource
    private ToolForkInfoRespository toolForkInfoRespository;

    @Resource
    private EntryExitRecordRepository entryExitRecordRepository;

    @Resource
    private ToolPartRespository toolPartRespository;

    public Object toolTypeAnalysis(ToolTypeAnalysisReq toolTypeAnalysisReq) throws ParseException {
        Map<String , Object> retMap = new HashMap<>();
        //按照类型获取库存情况
        List<ToolTypeAnalysisVo> toolTypeStockList = new ArrayList<>();
        //一级仓库刀具消耗情况
        List<ToolTypeAnalysisVo> toolTypeUsingList = new ArrayList<>();
        if(StringUtil.isEmpty(toolTypeAnalysisReq.getStartTime()) || ObjectUtil.isNull(toolTypeAnalysisReq.getStartTime())){
            //默认查询前7天的数据
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            toolTypeAnalysisReq.setStartTime(sdf.format(new Date()));
            //获取7天前的时间
            Calendar c = Calendar.getInstance();
            c.add(Calendar.DATE, - 7);
            Date monday = c.getTime();
            toolTypeAnalysisReq.setEndTime(sdf.format(monday));
        }
        //获取日期范围
        List<String> dateList = CommonUtil.findEveryDay(toolTypeAnalysisReq.getStartTime(), toolTypeAnalysisReq.getEndTime());
        dateList.forEach(date -> {
            ToolTypeAnalysisVo toolTypeStock = new ToolTypeAnalysisVo();
            ToolTypeAnalysisVo toolTypeUsing = new ToolTypeAnalysisVo();
            toolTypeStock.setDate(date);
            toolTypeUsing.setDate(date);
            List<ToolTypeAnalysisSimpleVo> stockChildList = new ArrayList<>();
            List<ToolTypeAnalysisSimpleVo> usingChildList = new ArrayList<>();
            toolTypeAnalysisReq.getTypeList().forEach(toolTypeAnalysisSimpleReq -> {
                ToolTypeAnalysisSimpleVo toolTypeStockSimpleVo = new ToolTypeAnalysisSimpleVo();
                ToolTypeAnalysisSimpleVo toolTypeUsingSimpleVo = new ToolTypeAnalysisSimpleVo();

                Date now = new Date();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String nowFormat = simpleDateFormat.format(now);

                if(nowFormat.equals(date)){
                    toolTypeStockSimpleVo.setTypeName(toolTypeAnalysisSimpleReq.getTypeName());
                    toolTypeStockSimpleVo.setNumber(toolPartRespository.countByType(toolTypeAnalysisSimpleReq.getType()));
                }else {
                    toolTypeStockSimpleVo.setTypeName(toolTypeAnalysisSimpleReq.getTypeName());
                    List<ToolTypeStockVo> typeStock   = toolForkInfoRespository.countLocationNumber(toolTypeAnalysisSimpleReq.getType() , date);
                    toolTypeStockSimpleVo.setNumber(typeStock.stream().filter(toolTypeStockVo ->ObjectUtil.isNotNull(toolTypeStockVo.getNumber())).mapToInt(ToolTypeStockVo::getNumber).sum());
                }

                toolTypeUsingSimpleVo.setTypeName(toolTypeAnalysisSimpleReq.getTypeName());
                List<ToolTypeStockVo> typeUsing = entryExitRecordRepository.countUsingNumber(toolTypeAnalysisSimpleReq.getType() , date);
                toolTypeUsingSimpleVo.setNumber(typeUsing.stream().filter(toolTypeStockVo ->ObjectUtil.isNotNull(toolTypeStockVo.getNumber())).mapToInt(ToolTypeStockVo::getNumber).sum());

                stockChildList.add(toolTypeStockSimpleVo);
                usingChildList.add(toolTypeUsingSimpleVo);
                toolTypeStock.setList(stockChildList);
                toolTypeUsing.setList(usingChildList);
            });
            toolTypeStockList.add(toolTypeStock);
            toolTypeUsingList.add(toolTypeUsing);
        });
        retMap.put("toolTypeStockList" , toolTypeStockList);
        retMap.put("toolTypeUsingList" , toolTypeUsingList);
        return retMap;
    }

//    public Object macToolAnalysis(MacToolAnalysisReq macToolAnalysisReq) {
//        return null;
//    }
}
